'use strict';

const Controller = require('egg').Controller;
const chrome = require('../util/chrome')

class HomeController extends Controller {
  async index() {

    let { ctx } = this

    let userData = ctx.query




    ctx.validate({
      type: {
        type: 'string'
      },
      project: {
        type: 'string'
      },
      path: {
        type: 'string'
      }
    }, userData);

    let hub = this.config.hub[userData.type]
    if(!hub) {
      ctx.body = {
        code: 500,
        msg:'不支持的类型'
      }

      return
    }

    let url = chrome.concatHubData({
      hub,
      project: userData.project,
      path: userData.path
    })

    let gitData = await chrome.get(url)


    if(gitData.code === 200) {
      ctx.body = {
        code: 200,
        data: gitData.data
      };
    } else {

      ctx.body = {
        response: gitData.response,
        code: 500,
        status: gitData.error.response.status,
        statusText: gitData.error.response.statusText,
        msg: '请检查路径 ' + url + ' 是否正确'
      }

    }


  }
}

module.exports = HomeController;
